import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { getBaseUrl } from '@/services/urlconst';
import { commonManageSearchParams, getTenantID, getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { getHomeSearchList, getHomeTabSwitch } from '../mconfig';
const baseUrl = getBaseUrl();
// const baseUrl = 'http://192.168.153.246:31019'; //旭潮本地
// const baseUrl = '/deleGateTest';

const contractWarnService = (curRes: Record<string, any>, callback: (res: any) => void) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.ALLORDER_TYPE}`, //工单类型
  ];

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = getHomeTabSwitch(curRes);

  /** 获取搜索参数 */
  const searchListParams = getHomeSearchList(queryTypeObj);

  /** 获取列表数据  */
  const { loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        // current: 1,
        // pageSize: 10,
        sort: 'readTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        ...opt,
      };
      const searchParams: Record<string, any> = commonManageSearchParams(searchListParams, requestBody, {
        dicTransName: 'orderType',
        orgTransName: `${ISearchTranslateAll.OWNER_DEPT}`,
        // areaTransName: `${ISearchTranslateAll.AREA_TRANS}`,
      });

      searchParams.whereParams.push({
        fieldName: 'readFlag',
        valueInfo: 0,
        fieldType: 'Integer',
        fieldParam: 1,
        fieldUse: 0,
        isSearch: 1,
        searchType: 1,
        seq: 0,
      });
      const requestURL = `${baseUrl}/warnController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, searchParams);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      // defaultPageSize: 10,
      onSuccess: callback,
    },
  );

  /* 获取页面数据 */
  const getContractData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    if (request?.readTime) {
      const [readTimeStart, readTimeEnd] = request.readTime;
      request['startTime'] = readTimeStart.format('YYYY-MM-DD 00:00:00');
      request['endTime'] = readTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['readTime'];
    }

    if (request?.ownerID && request?.ownerID?.length > 0) {
      request['ownerID'] = request?.ownerID?.toString();
    } else {
      delete request['ownerID'];
    }

    run({
      ...request,
    });
  };

  return {
    loading,
    getContractData,
    reloadRefresh: refresh,
  };
};

/**
 * 获取我的编撰
 * @param getContractWarnDataCallback
 * @returns
 */
const getContractWarnDataSercvice = (curRes: Record<string, any>, getContractWarnDataCallback: (res: any) => void) => {
  const getContractWarnData = useRequest(
    (opt: any) => {
      if (!curRes?.['/warnController/contractHomePage'] || !curRes?.['/warnController/dataGrid/{fieldName}']) return;

      /**  需要获取的数据字典 */
      const loadMdicIds: string[] = [
        `${getTenantID()}${ILoadMapTypeAll.ALLORDER_TYPE}`, //工单类型
      ];

      /**获取权限参数 */
      const queryTypeObj: Record<string, any> = getHomeTabSwitch(curRes);

      /** 获取搜索参数 */
      const searchListParams = getHomeSearchList(queryTypeObj);

      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        // current: 1,
        // pageSize: 10,
        sort: 'readTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        ...opt,
      };
      const searchParams: Record<string, any> = commonManageSearchParams(searchListParams, requestBody, {
        dicTransName: 'orderType',
        orgTransName: `${ISearchTranslateAll.OWNER_DEPT}`,
        // areaTransName: `${ISearchTranslateAll.AREA_TRANS}`,
      });

      searchParams.whereParams.push({
        fieldName: 'readFlag',
        valueInfo: 0,
        fieldType: 'Integer',
        fieldParam: 1,
        fieldUse: 0,
        isSearch: 1,
        searchType: 1,
        seq: 0,
      });
      const requestURL = `${baseUrl}/warnController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, searchParams);
    },
    {
      manual: true,
      onSuccess: getContractWarnDataCallback,
    },
  );

  return {
    getContractWarnDataRun: getContractWarnData?.run,
    getContractWarnDataLoading: getContractWarnData?.loading,
  };
};

/**
 * 获取详情
 * @param callback  获取详情回调
 * @returns
 */
const getWarnByIDService = (callback: (res: any) => void) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.ALLORDER_TYPE}`, //工单类型
  ];

  const getWarnByID = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/warnController/getMwarnByID`;
      const payload: Record<string, any> = {
        ...params,
        dicTypeIds: loadMdicIds?.toString(),
        dicTransName: 'orderType',
        orgTransName: `${ISearchTranslateAll.OWNER_DEPT}`,
        ownerID: getUserID(),
      };
      return requestFunComponFiex('PUT', personUrl, payload);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getWarnByIDRun: getWarnByID?.run,
    getWarnByIDLoading: getWarnByID?.loading,
    getWarnByIDInfo: getWarnByID?.data?.obj,
  };
};

/**
 *  设置已读
 * @param callback  设置已读回调
 * @returns
 */
const getWarnSetReadService = (callback: (res: any) => void) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.ALLORDER_TYPE}`, //工单类型
  ];

  const getWarnSetRead = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/warnController/setRead`;
      const payload: Record<string, any> = {
        ...params,
        dicTypeIds: loadMdicIds?.toString(),
        dicTransName: 'orderType',
        orgTransName: `${ISearchTranslateAll.OWNER_DEPT}`,
        ownerID: getUserID(),
        readFlag: 1,
      };
      return requestFunComponFiex('PUT', personUrl, payload);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getWarnSetReadRun: getWarnSetRead?.run,
    getWarnSetReadLoading: getWarnSetRead?.loading,
  };
};

export { contractWarnService, getWarnByIDService, getWarnSetReadService, getContractWarnDataSercvice };
